package com.example.myapplication

import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.concurrent.thread

class CoroutineActivity : AppCompatActivity() {

    private  val TAG = "CoroutineActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coroutine)
        findViewById<Button>(R.id.btn).setOnClickListener {
            runBlocking {
                Log.d(TAG, "begin: ")
                val job = launch {
                    withContext(NonCancellable) {
                        repeat(200){i->
                            println("job: I'm sleeping $i ...")
                            delay(500L)
                        }
                        Log.d(TAG, "thread : " + Thread.currentThread().name)
                    }
                }
                delay(1000)
                Log.d(TAG, "start: ")
                job.cancel()
                Log.d(TAG, "end: ")
                test()
            }
            Log.d(TAG, "complete: ")
        }
    }

    suspend fun test(name:String){

    }
}
